package com.cmc.cloud.cmclink.doc.service.impl;

import com.cmc.cloud.cmclink.agt.api.base.req.document.DocumentAgreementRateInfoReqDTO;
import com.cmc.cloud.cmclink.agt.api.base.resp.document.DocumentAgreementRateInfoRespDTO;
import com.cmc.cloud.cmclink.doc.api.document.emums.ExImTypeEnum;
import com.cmc.cloud.cmclink.doc.entity.BlnoCntrDO;
import com.cmc.cloud.cmclink.doc.entity.BlnoDO;
import com.cmc.cloud.cmclink.doc.entity.CalculateOperateLogDO;
import com.cmc.cloud.cmclink.doc.mapper.CalculateOperateLogMapper;
import com.cmc.cloud.cmclink.doc.service.BlnoCalculateOperateService;
import com.cmc.cloud.cmclink.doc.util.GsonUtil;
import com.cmc.cloud.cmclink.mkt.dto.loc.CalLocReqVo;
import com.cmc.cloud.cmclink.mkt.dto.loc.CalLocRespVo;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lzp
 * @date 2025/09/04
 **/
@Service
public class BlnoCalculateOperateServiceImpl implements BlnoCalculateOperateService {

    @Resource
    private CalculateOperateLogMapper calculateOperateLogMapper;

    /**
     * 保存调用营销接口的日志信息
     *
     * @param blnoMap          提单信息
     * @param calLocReqVoList  营销的请求参数
     * @param calLocRespVoList 营销的返回参数
     * @param blnoCntrMap      箱信息
     * @param calType          计算类型 local charge ex
     * @param status           计算结果
     * @param failMessage      失败信息
     * @param imExType         进出口类型IM EX
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Override
    public void saveCalLog(Map<Long, List<BlnoDO>> blnoMap, List<CalLocReqVo> calLocReqVoList, List<CalLocRespVo> calLocRespVoList, Map<Long, List<BlnoCntrDO>> blnoCntrMap, String calType, String status, String failMessage, String imExType) {
        List<CalculateOperateLogDO> calculateOperateLogDoList = new ArrayList<>(64);
        for (CalLocReqVo calLocReqVo : calLocReqVoList) {
            for (CalLocRespVo calLocRespVo : calLocRespVoList) {
                if (calLocReqVo.getBlnoId().equals(calLocRespVo.getBlnoId())) {
                    CalculateOperateLogDO calculateOperateLog = new CalculateOperateLogDO();
                    calculateOperateLog.setBusinessId(Long.valueOf(calLocReqVo.getQueryId()));
                    calculateOperateLog.setBusinessNo(calLocReqVo.getBlno());
                    if (ExImTypeEnum.EX.getValue().equals(imExType)) {
                        calculateOperateLog.setPort(blnoMap.get(calLocReqVo.getBlnoId()).get(0).getPolCode());
                    } else {
                        calculateOperateLog.setPort(blnoMap.get(calLocReqVo.getBlnoId()).get(0).getPodCode());
                    }
                    calculateOperateLog.setCalculateType(calType);
                    calculateOperateLog.setOperationStatus(status);
                    calculateOperateLog.setOperationFailDesc(failMessage);
                    Gson gson = GsonUtil.getGson();
                    String reqJson = gson.toJson(calLocReqVo);
                    String rpsJson = gson.toJson(calLocRespVo);
                    String cntrJson = gson.toJson(blnoCntrMap.getOrDefault(calLocReqVo.getBlnoId(), new ArrayList<>()));
                    calculateOperateLog.setReceiveJson(rpsJson);
                    calculateOperateLog.setSendJson(reqJson);
                    calculateOperateLog.setContainerJson(cntrJson);
                    calculateOperateLogDoList.add(calculateOperateLog);
                }
            }
        }
        // 插入
        calculateOperateLogMapper.insertBatch(calculateOperateLogDoList);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Override
    public void saveMultiLocalCostLog(Map<String, List<BlnoDO>> blnoMap, ArrayList<DocumentAgreementRateInfoReqDTO> reqs, ArrayList<DocumentAgreementRateInfoRespDTO> resps, Map<Long, List<BlnoCntrDO>> blnoCntrMap, String calType, String status, String failMsg) {
        List<CalculateOperateLogDO> calculateOperateLogDoList = new ArrayList<>(64);
        for (DocumentAgreementRateInfoReqDTO req : reqs) {
            for (DocumentAgreementRateInfoRespDTO resp : resps) {
                if (req.getBlNo().equals(resp.getBlNo())) {
                    BlnoDO blnoDo = blnoMap.get(req.getBlNo()).get(0);
                    CalculateOperateLogDO calculateOperateLog = new CalculateOperateLogDO();
                    calculateOperateLog.setBusinessId(blnoDo.getId());
                    calculateOperateLog.setBusinessNo(req.getBlNo());
                    calculateOperateLog.setPort(req.getPortCode());
                    calculateOperateLog.setCalculateType(calType);
                    calculateOperateLog.setOperationStatus(status);
                    calculateOperateLog.setOperationFailDesc(StringUtils.isBlank(failMsg) ? resp.getErrorAll() : failMsg);
                    Gson gson = GsonUtil.getGson();
                    String reqJson = gson.toJson(req);
                    String rpsJson = gson.toJson(resp);
                    String cntrJson = gson.toJson(blnoCntrMap.getOrDefault(blnoDo.getId(), new ArrayList<>()));
                    calculateOperateLog.setReceiveJson(rpsJson);
                    calculateOperateLog.setSendJson(reqJson);
                    calculateOperateLog.setContainerJson(cntrJson);
                    calculateOperateLogDoList.add(calculateOperateLog);
                }
            }
        }
        // 插入
        calculateOperateLogMapper.insertBatch(calculateOperateLogDoList);
    }
}
